package com.systemsjr.mpf.account;

import com.systemsjr.mpf.R;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TableLayout;
import android.widget.TextView;

public class InstanceListAdapter extends SimpleCursorAdapter {

	public InstanceListAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to, int flags) {
		super(context, layout, c, from, to, flags);
		
		this.layout = layout;
	}

	private int layout;
	
	public InstanceListAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		
		this.layout = layout;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		final LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(layout, parent, false);
		return v;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		super.bindView(view, context, cursor);
		TextView budgetText = (TextView) view.findViewById(R.id.list_budget);
		
		double budget = 0.0;
		
		if(budgetText != null && budgetText.getText() != null && budgetText.getText().length() > 0){
			budget = Double.parseDouble(budgetText.getText().toString());
		}
		
		double spent = 0.0;
		
		TextView spentText = (TextView) view.findViewById(R.id.list_used_amount);
		
		if(spentText != null && spentText.getText() != null && spentText.getText().length() > 0){
			spent = Double.parseDouble(spentText.getText().toString());
		}
		
		TableLayout layout = (TableLayout) view.findViewById(R.id.acc_list_layout);
		TextView instanceText = (TextView) view.findViewById(R.id.list_instance);
		
		if(layout != null && budget >= spent){
			layout.setBackgroundColor(view.getResources().getColor(R.color.budget_color_green));
			budgetText.setBackgroundColor(view.getResources().getColor(R.color.budget_color_green));
			spentText.setBackgroundColor(view.getResources().getColor(R.color.budget_color_green));
			instanceText.setBackgroundColor(view.getResources().getColor(R.color.budget_color_green));
		} else if(layout != null){
			layout.setBackgroundColor(view.getResources().getColor(R.color.budget_color_red));
			budgetText.setBackgroundColor(view.getResources().getColor(R.color.budget_color_red));
			spentText.setBackgroundColor(view.getResources().getColor(R.color.budget_color_red));
			instanceText.setBackgroundColor(view.getResources().getColor(R.color.budget_color_red));
		}
	}



}
